<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formRobotTrait{

    public function index_robot(Content $content)
    {
        return $content->title('活跃系统')->description('需要先在 用户管理-用户 导入机器人')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_robot()->render()}</div>
HTML
            );
    }

    protected function form_robot(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->html(function () {
            $res9 = __system__paycode__tip__(9);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res9['tip']);
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">'.$res9['text'].'简单说明：每篇内容的喜欢数的多少是根据内容质量计算的，所以不会全都相同</div>';
        }, '');

        $form->slider('robot_action_like_rate', '内容喜欢')->help('当有用户发布内容时，会派遣机器人喜欢内容，如果你觉得点赞数少，就调大一点，反之调小。0为关闭此功能')->default(_between_(Settings::get('robot_action_like_rate', 100),0,100));
        $form->slider('robot_action_follow_rate', '用户关注')->help('当有用户发布内容时，会派遣机器人关注作者，如果你觉得关注数少，就调大一点，反之调小。0为关闭此功能')->default(_between_(Settings::get('robot_action_follow_rate', 100),0,100));
        $form->slider('robot_action_comment_like_rate', '评论喜欢')->help('当有用户发布评论时，会派遣机器人喜欢评论，如果你觉得喜欢数少，就调大一点，反之调小。0为关闭此功能')->default(_between_(Settings::get('robot_action_comment_like_rate', 100), 0, 100));
        $form->action('settings/action/robot');
        return $form;
    }

    public function robot(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('robot_action_like_rate', $request->input('robot_action_like_rate'), true);
        Settings::set('robot_action_follow_rate', $request->input('robot_action_follow_rate'), true);
        Settings::set('robot_action_comment_like_rate', $request->input('robot_action_comment_like_rate'), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
